import BaseModel from "../base/model/BaseModel";
import GuideEventName from "./GuideEventName";
import XlsxData from "../config/XlsxData";
export enum Guide_infoEnum{
	guideID,
	posInfo,
	endInfo,
	des,
	fingerDir,
	descDir,
	
}

import GuideItemModel from "./GuideItemModel";
import BaseView from "../base/view/BaseView";
import GameManager from "../game/GameManager";
export default class GuideManager extends BaseModel {

    private static ins: GuideManager;

    static instance(): GuideManager {
        if (!this.ins) {
            this.ins = new GuideManager();
        }
        return this.ins;
    }


    private guideItemModel: GuideItemModel;

    private step: number = -1;

    private list: number[] = []

    private checkMap = {}

    private layerMap = {}
    constructor() {
        super();
        this.guideItemModel = new GuideItemModel();
        let config: XlsxData = GameManager.instance().getConfig('guide_info')
        config.forEach((key,value:any[])=>{
            let endInfo:string = value[Guide_infoEnum.endInfo];
            let endList = endInfo.split(':')
            if(!this.layerMap[endList[0]]){
                this.layerMap[endList[0]] = []
            }
            this.layerMap[endList[0]].push(endList[1])

        })
    }

    isHaveGuide() {
        return this.step >= 0;
    }
    init(layer:BaseView) {
        let layerName = layer.name;
        if(this.layerMap[layerName]){
            this.checkMap[layerName] = {}
            let list = this.layerMap[layerName]
            for (let index = 0; index < list.length; index++) {
                const funcName = list[index];
                this.checkMap[layerName][funcName] = layer[funcName];
            }
        }
        
    }

    click(){
        
    }

    start(guideID: number) {
        if(guideID > 0){
            let config: XlsxData = GameManager.instance().getConfig('guide_info')
            if (config) {
                this.list = config.getIndex(guideID)
                this.step = 0;
                this.publish(GuideEventName.START)
                this.update();
            }
        }

    }

    getItemModel() {
        if (this.list.length > 0 && this.step >= 0) {
            let config: XlsxData = GameManager.instance().getConfig('guide_info')
            let id = this.list[this.step]
            this.guideItemModel.init(id, config.getRowData(id))
            return this.guideItemModel;
        }
        return null;
    }

    end() {
        this.step = -1;
        this.publish(GuideEventName.END)
    }

    update() {
        this.publish(GuideEventName.UPDATE)
    }

    check(layerName: string, funcName: string,id?:any) {
        if (!this.isHaveGuide()) {
            return true;
        }
        if (this.guideItemModel.check(layerName, funcName,id)) {
            this.next();
            return true;
        }else{
            if(this.guideItemModel.isForce()){
                return false;
            }else{
                this.end();
                return true;
            }

        }
    }

    next(){
        this.step++;
        if (this.step >= this.list.length) {
            this.end();
        } else {
            this.update();
        }
    }
}
